package com.example.test;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.Page;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
@Theme("test")
public class PaginaPrincipal extends UI {

	@WebServlet(value = "/*", asyncSupported = true)
	@VaadinServletConfiguration(productionMode = false, ui = PaginaPrincipal.class)
	public static class Servlet extends VaadinServlet {
	}

	Login pl=new Login();
	AltaEmpresa ae= new AltaEmpresa();
	
	VerticalLayout vLayoutPrincipal = new VerticalLayout();
	VerticalLayout vLayoutTitulo = new VerticalLayout();

	Panel panelLogin= new Panel();
	Panel panelRegistro= new Panel();
	HorizontalLayout hLayouBotones = new HorizontalLayout();
	VerticalLayout vLayoutResultado = new VerticalLayout();
	VerticalLayout vLayoutSecundario = new VerticalLayout();
	VerticalLayout vLayoutRegistro = new VerticalLayout();
	Notification errorUsu = new Notification("Usuario Incorrecto", Notification.Type.ERROR_MESSAGE);

	@Override
	protected void init(VaadinRequest request) {

				
		cargarPagina();
		
	
	}
	public void AceptarLogin(){
		
		VerticalLayout vL;
		try {
			vL = pl.Aceptar();
		
		if (vL != null) {
			vLayoutPrincipal.setVisible(false);
			vLayoutSecundario = new VerticalLayout();
			vLayoutSecundario.removeAllComponents();
			vLayoutSecundario.addComponent(vL);
			setContent(vLayoutSecundario);
		} else
			
			errorUsu.show(Page.getCurrent());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
		
	
	
	public void cargarRegistro(){
		
		vLayoutPrincipal.setVisible(false);
		vLayoutRegistro.removeAllComponents();
		panelRegistro = pl.Registro();
		panelRegistro.setSizeUndefined();
		vLayoutRegistro.addComponent(vLayoutTitulo);
		vLayoutRegistro.addComponent(panelRegistro);
		vLayoutRegistro.setComponentAlignment(vLayoutTitulo, Alignment.TOP_CENTER);
		vLayoutRegistro.setComponentAlignment(panelRegistro, Alignment.MIDDLE_CENTER);

		setContent(vLayoutRegistro);
		
	}
	
	public void cargarPagina(){
		
		vLayoutResultado.setVisible(false);
		vLayoutPrincipal.removeAllComponents();
		vLayoutPrincipal.setVisible(true);
		vLayoutTitulo = pl.LayoutTitulo();
		vLayoutTitulo.setHeight("200px");
		panelLogin = pl.PanelLogin();
		vLayoutPrincipal.addComponent(vLayoutTitulo);
		vLayoutPrincipal.setComponentAlignment(vLayoutTitulo,Alignment.TOP_CENTER);
		vLayoutPrincipal.addComponent(panelLogin);
		vLayoutPrincipal.setComponentAlignment(panelLogin,Alignment.TOP_CENTER);
		
		
		setContent(vLayoutPrincipal);
	}
	
	public void cancelar(){
		MenuPrincipal m = new MenuPrincipal();
		VerticalLayout layout = m.menu();
		vLayoutPrincipal.setVisible(false);
		vLayoutSecundario = new VerticalLayout();
		vLayoutSecundario.removeAllComponents();
		vLayoutSecundario.addComponent(layout);
		setContent(vLayoutSecundario);
	}

	



	public void logout() {
		
		cargarPagina();
		
	}

}
